﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ahi.Config;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System.Data;
using MongoDB.Bson;
using Ahi.Entity;
using Ahi.Entity.Interfaces;

namespace Ahi
{
    public class MongoAdaptor
    {
        private MongoDatabase db;

        #region Singleton Instance
        private MongoAdaptor()
        {
        }

        private static MongoAdaptor _instance = null;
        public static MongoAdaptor Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new MongoAdaptor();
                return _instance;
            }
        }
        #endregion


        public bool Connect(string sunucu = null)
        {
            try
            {
                string connectionString = string.Format("mongodb://{0}/Dernek",
                    string.IsNullOrEmpty(sunucu) ?
                    AppConfig.Current.VeritabaniSunucusu :
                    sunucu);

                db = MongoDatabase.Create(connectionString);
                if (db == null)
                    return false;
            }
            catch
            {
                throw;
            }
            return true;
        }

        public static MongoDatabase DB
        {
            get
            {
                if (MongoAdaptor.Instance.db == null)
                    MongoAdaptor.Instance.Connect();
                return MongoAdaptor.Instance.db;
            }
        }

        public MongoCollection Coll(string collectionName)
        {
            return db.GetCollection(collectionName);
        }

        public MongoCollection<T> Coll<T>()
        {
            return db.GetCollection<T>(Utilities.MongoUtil.GetDBCollectionName(typeof(T)));
        }

        public List<T> GetData<T>()
        {
            return Coll<T>().Find(Query.EQ("Passive", false)).ToList();
        }

        public T GetDataById<T>(ObjectId objId)
        {
            return Coll<T>().Find(Query.EQ("_id", objId)).FirstOrDefault();
        }

        public List<T> GetDataByField<T>(string fieldName, object fieldValue)
        {
            return Coll<T>().Find(Query.EQ(fieldName, BsonValue.Create(fieldValue))).ToList();
        }

        public T GetFirst<T>()
        {
            return GetData<T>().FirstOrDefault();
        }

        public bool Save<T>(T entity)
        {
            SafeModeResult result = Coll<T>().Save<T>(entity);
            if (result == null) return false;
            return result.Ok;
        }

        public bool Save<T>(List<T> entityList)
            where T : IEntityBase
        {
            bool result = true;
            foreach (var item in entityList)
            {
                bool subResult = item.Save();
                result = result && subResult;
            }
            return result;
        }

        internal void Reset()
        {
            db.Drop();
        }
    }
}
